﻿/**
 * 创建元素工具类
 */
/**
 * ************ 创建text ****************
 */
function createInput(className, placeholder, text) {
    var input = $('<input type="text" />');

    $(input).addClass(className);

    $(input).attr("random-id", getRandomCode());

    $(input).attr("placeholder", placeholder);

    $(input).attr("value", text);

    return input;
}

/**
 * ************ 创建button ****************
 */

function createButton(className, title, clickAction) {
    var button = $("<button>");

    $(button).addClass(className);

    $(button).html(title);

    if (clickAction != null) {

        $(button).click(function () {

            clickAction($(this));

        });
    }

    return button;
}

/**
 * ************ 创建a ****************
 */

function createA(className, text, href, target) {
    var a = $("<a>");

    $(a).attr("href", href);

    $(a).html(text);

    if (!isNull(target)) {
        $(a).attr("target", target);
    }

    $(a).addClass(className);

    return a;

}

/**
 * ************ 创建select ****************
 */
function createSelect(className, optionList, selectItem) {
    var select = $("<select>");

    $(select).addClass(className);

    if (selectItem == null) {
        selectItem = optionList[0];
    }

    $.each(optionList, function (index, ele) {

        var option = $("<option>");

        if (selectItem != null && selectItem == ele) {
            $(option).attr("selected", "selected");
        }

        $(option).html(ele);

        $(option).attr("value", ele);

        $(option).appendTo(select);

    });

    $(select).attr("lastSelectItem", selectItem);

    return select;

}
/**
 * ************ 创建label ****************
 */

function createLabel(className, text) {
    var lbl = $("<label>");

    $(lbl).html(text);

    $(lbl).addClass(className);

    return lbl;
}
/**
 * ************ 创建div ****************
 */
function createDiv(id, className, appendTo) {

    var div = $("<div>");

    $(div).attr("id", id);

    $(div).addClass(className);

    if (!isNull(appendTo)) {
        $(div).appendTo(appendTo);
    }

    return div;

}

/**
 * ************************************** 创建script
 * ****************************************
 */

function createScript(id, html, appendTo) {
    var script = $('<script type="text/javascript" >');

    $(script).html(html);

    $(script).attr("id", id);

    if (!isNull(appendTo)) {
        $(script).appendTo(appendTo);
    }

    return script;

}

function appendBreak(ele) {
    $(ele).insertAfter("12323");

}
// 生成随机数
function getRandomCode(prefix) {
    /*
     * var id = "id" + ((new Date().getTime()) ^ Math.random());
     *
     * if (!isNull(prefix)) { id = prefix + "_" + id; }
     */
    var id = rand(10);

    return id
}
function rand(length, current) {

    current = current ? current : '';

    return length ? rand(--length,
        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"
            .charAt(Math.floor(Math.random() * 60))
        + current) : current;

}
// 打印日志
function log() {
    if (isDebug()) {
        for (var i = 0; i < arguments.length; i++) {
            console.log(arguments[i]);
        }
    }
}
// 判断是否为空
function isNull(arg) {
    if (arg == null || arg.length == 0 || arg == "" || typeof arg == "undefined") {

        return true;
    }
    return false;
}

function isJson(arg) {

    if (isNull(arg)) {
        return false
    }
    if (typeof arg == "object") {
        return true;
    }
    try {
        eval("var a = " + arg);
    } catch (e) {
        return false;
    }
    if (typeof(a) != "object") {
        return false;
    }
    return true;
}

// 是否在调试
function isDebug() {
    return true;
}

// 使用layer展示错误提示
function tipError(msg, to) {
    layer.tips(msg, to);
}

function layerAlertMsg(msg) {
    layer.alert(msg, {
        skin: 'layui-layer-lan'
        , closeBtn: 0
        , shift: 4
    });
}